package com.example.testapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;


public class minePage extends Fragment {

    protected TextView tv,setting,phase,tvCheckName,tvArticleManagement;
    private LocalReceiver localReceiver;
    private LocalBroadcastManager localBroadcastManager;
    private LinearLayout myMessageLinear;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_mine_page, container, false);

        tv = view.findViewById(R.id.textView28);
        setting = view.findViewById(R.id.textView3);
        phase =view.findViewById(R.id.textView29);
        tvCheckName = view.findViewById(R.id.tvCheckName);
        tvArticleManagement = view.findViewById(R.id.textView26);
        myMessageLinear = view.findViewById(R.id.myMessageLinear);

        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), Question_page.class));
            }
        });

        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), ButtonActivity.class));
            }
        });

        phase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), ProjectPhaseActivity.class));
            }
        });
        tvArticleManagement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), ArticleManagementActivity.class));
            }
        });
        myMessageLinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), QuestionDetailActivity.class));
            }
        });

        IntentFilter filterLocal = new IntentFilter();
        filterLocal.addAction("com.example.LOCAL");
        localReceiver = new LocalReceiver();
        localBroadcastManager=LocalBroadcastManager.getInstance(getActivity());
        localBroadcastManager.registerReceiver(localReceiver,filterLocal);

        return view;
    }

    public class LocalReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            tvCheckName.setText(intent.getStringExtra("str"));
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(localReceiver);
    }

}
